/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.front.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.front.entity.Report;
import com.thinkgem.jeesite.modules.front.service.ReportService;

/**
 * 测试报告内容管理Controller
 * @author 朱俊吉
 * @version 2017-12-04
 */
@Controller
@RequestMapping(value = "${adminPath}/front/report")
public class ReportController extends BaseController {

	@Autowired
	private ReportService reportService;
	
	@ModelAttribute
	public Report get(@RequestParam(required=false) String id) {
		Report entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = reportService.get(id);
		}
		if (entity == null){
			entity = new Report();
		}
		return entity;
	}
	
	@RequiresPermissions("front:report:view")
	@RequestMapping(value = {"list", ""})
	public String list(Report report, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Report> page = reportService.findPage(new Page<Report>(request, response), report); 
		model.addAttribute("page", page);
		return "modules/front/reportList";
	}

	@RequiresPermissions("front:report:view")
	@RequestMapping(value = "form")
	public String form(Report report, Model model) {
		model.addAttribute("report", report);
		return "modules/front/reportForm";
	}

	@RequiresPermissions("front:report:edit")
	@RequestMapping(value = "save")
	public String save(Report report, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, report)){
			return form(report, model);
		}
		reportService.save(report);
		addMessage(redirectAttributes, "保存报告内容成功");
		return "redirect:"+Global.getAdminPath()+"/front/report/?repage";
	}
	
	@RequiresPermissions("front:report:edit")
	@RequestMapping(value = "delete")
	public String delete(Report report, RedirectAttributes redirectAttributes) {
		reportService.delete(report);
		addMessage(redirectAttributes, "删除报告内容成功");
		return "redirect:"+Global.getAdminPath()+"/front/report/?repage";
	}

}